package com.sk.waste.app.activitys

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.text.TextUtils
import android.view.Gravity
import android.widget.Button
import android.widget.CheckBox
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.lc.smc.lib.annotations.LCBindView
import com.lc.smc.lib.interfaces.LCInterface
import com.lc.smc.lib.utils.LCBaseUtil
import com.lc.smc.lib.utils.LCSPUtil
import com.sk.pay.lib.PayConfig
import com.sk.pay.lib.PaymentUtil
import com.sk.pay.lib.aipay.PayResult
import com.sk.waste.app.R
import com.sk.waste.app.application.MyApplication
import com.sk.waste.app.bases.BaseActiivity
import com.sk.waste.app.config.AppConfig
import com.sk.waste.app.config.NetWorkConfig
import com.sk.waste.app.enums.IdenEmum
import com.sk.waste.app.enums.PayEmum
import com.sk.waste.app.models.MarketModel
import com.sk.waste.app.models.VipModel
import com.sk.waste.app.utils.OkHttpUtil
import com.sk.waste.app.utils.PermissionUtil
import com.sk.waste.app.views.PaymentWindow
import okhttp3.Call
import org.json.JSONObject
import java.io.IOException

/**
 *  @time 2025/4/25  17:11
 *  @desc 开通vip
 */
class VipBuyActivity : BaseActiivity() {

    @LCBindView(R.id.ac_root_view)
    private lateinit var rootView: ConstraintLayout

    @LCBindView(R.id.ac_vipbuy_month_layout)
    private lateinit var layoutMonth: FrameLayout

    @LCBindView(R.id.ac_vipbuy_month_btt)
    private lateinit var bttMonth: Button

    @LCBindView(R.id.ac_vipbuy_oneyear_layout)
    private lateinit var layoutOneYear: FrameLayout

    @LCBindView(R.id.ac_vipbuy_oneyear_btt)
    private lateinit var bttOneYear: Button

    @LCBindView(R.id.ac_vipbuy_twoyear_layout)
    private lateinit var layoutTwoYear: FrameLayout

    @LCBindView(R.id.ac_vipbuy_twoyear_btt)
    private lateinit var bttTwoYear: Button

    @LCBindView(R.id.ac_vipbuy_first_price)
    private lateinit var ttvFirstPrice: TextView

    @LCBindView(R.id.ac_vipbuy_first_lab)
    private lateinit var ttvFirstLab: TextView

    @LCBindView(R.id.ac_vipbuy_second_price)
    private lateinit var ttvSecondPrice: TextView

    @LCBindView(R.id.ac_vipbuy_second_lab)
    private lateinit var ttvSecondLab: TextView

    @LCBindView(R.id.ac_vipbuy_third_price)
    private lateinit var ttvThirdPrice: TextView

    @LCBindView(R.id.ac_vipbuy_third_lab)
    private lateinit var ttvThirdLab: TextView

    private lateinit var paymentWindow: PaymentWindow

    private var PAY_TYPE = ""

    private val vipList = arrayListOf<VipModel>()


    override fun onCreate(savedInstanceState: Bundle?) {
        setContentView(R.layout.activity_vipbuy)
        super.onCreate(savedInstanceState)
        setBack()
        setTitleView("开通VIP")
        initPopuwindow()
        getVipList()
        //半年
        layoutMonth.setOnClickListener {
            if (vipList.size != 0) {
                val firstVip = vipList[1]
                PAY_TYPE = firstVip.code
                showPaymentWindow(firstVip.price, firstVip.name)
            } else {
                PAY_TYPE = PayEmum.HALF_YEAR.type
                showPaymentWindow(PayEmum.HALF_YEAR.price, "VIP半年")
            }
        }
        //半年
        bttMonth.setOnClickListener {
            if (vipList.size != 0) {
                val firstVip = vipList[1]
                PAY_TYPE = firstVip.code
                showPaymentWindow(firstVip.price, firstVip.name)
            } else {
                PAY_TYPE = PayEmum.HALF_YEAR.type
                showPaymentWindow(PayEmum.HALF_YEAR.price, "VIP半年")
            }
        }
        //一年
        layoutOneYear.setOnClickListener {
            if (vipList.size != 0) {
                val secondVip = vipList[2]
                PAY_TYPE = secondVip.code
                showPaymentWindow(secondVip.price, secondVip.name)
            } else {
                PAY_TYPE = PayEmum.ONE_YEAR.type
                showPaymentWindow(PayEmum.ONE_YEAR.price, "VIP一年")
            }
        }
        //一年
        bttOneYear.setOnClickListener {
            if (vipList.size != 0) {
                val secondVip = vipList[2]
                PAY_TYPE = secondVip.code
                showPaymentWindow(secondVip.price, secondVip.name)
            } else {
                PAY_TYPE = PayEmum.ONE_YEAR.type
                showPaymentWindow(PayEmum.ONE_YEAR.price, "VIP一年")
            }
        }
        //两年
        layoutTwoYear.setOnClickListener {
            if (vipList.size != 0) {
                val thirdVip = vipList[3]
                PAY_TYPE = thirdVip.code
                showPaymentWindow(thirdVip.price, thirdVip.name)
            } else {
                PAY_TYPE = PayEmum.TWO_YEARS.type
                showPaymentWindow(PayEmum.TWO_YEARS.price, "VIP二年")
            }
        }
        //两年
        bttTwoYear.setOnClickListener {
            if (vipList.size != 0) {
                val thirdVip = vipList[3]
                PAY_TYPE = thirdVip.code
                showPaymentWindow(thirdVip.price, thirdVip.name)
            } else {
                PAY_TYPE = PayEmum.TWO_YEARS.type
                showPaymentWindow(PayEmum.TWO_YEARS.price, "VIP二年")
            }
        }
    }

    private fun setView() {
        if (vipList.size == 0) {
            return
        }
        ttvFirstPrice.tag = vipList[1].code
        ttvFirstPrice.text = vipList[1].price
        ttvFirstLab.text = vipList[1].name
        ttvSecondPrice.text = vipList[2].code
        ttvSecondPrice.text = vipList[2].price
        ttvSecondLab.text = vipList[2].name
        ttvThirdPrice.tag = vipList[3].code
        ttvThirdPrice.text = vipList[3].price
        ttvThirdLab.text = vipList[3].name
    }


    /**
     *  @time 2025/4/28  15:13
     *  @desc 支付
     */
    private fun initPopuwindow() {
        paymentWindow = PaymentWindow()
        paymentWindow.init(this)
        paymentWindow.setPopuOnclickListener(object : LCInterface.LcCallBack {
            override fun callBack(vararg args: Any?) {
                val type = args[0] as String
                if (type == "0") //支付宝
                {
                    paymentWindow.creatAliPay(PAY_TYPE)
                } else if (type == "1") {//微信
                    paymentWindow.creatWxPay(PAY_TYPE)
//                    val paymentUtil = PaymentUtil()
//                    paymentUtil.wechatPay(mContext, orderInfo)
                } else {// 回调
                    MyApplication.instance.getUserInfo()!!.vip = "VIP"
                    LCBaseUtil.showToast(mContext, "支付成功，您的VIP权益已开通可正常使用！")
                }
            }
        })
    }


    private fun getVipList() {
        val url = NetWorkConfig.NET_URL_ROOT + NetWorkConfig.NET_URL_VIP_LIST
        val token = LCSPUtil[mContext, AppConfig.SP_ACCESS_TOKEN]

        val httpUtil = OkHttpUtil()
        httpUtil.httpRequestGet(url, token, object : LCInterface.LcCallBackListener {
            override fun onFailure(call: Call, e: IOException) {
                LCBaseUtil.showToast(mContext, "获取失败，" + e.message)
            }

            @Throws(IOException::class)
            override fun onSuccess(call: Call, response: String) {
                try {
                    val jsonObject = JSONObject(response)
                    val success = jsonObject.getBoolean("success")
                    val msg = jsonObject.getString("message")
                    val data = jsonObject.getJSONArray("data")
                    vipList.clear()
                    if (success) {
                        for (index in 0 until data.length()) {
                            val item = VipModel().apply { getModelInfo(data.getJSONObject(index)) }
                            vipList.add(item)
                        }
                        runOnUiThread { setView() }
//                        LCLogger.d(TAG, "获取成功！,返回信息：$jsonObject")
                    } else {
                        LCBaseUtil.showToast(mContext, "获取失败，$msg")
                    }
                } catch (e: Exception) {
                    e.printStackTrace()
                    LCBaseUtil.showToast(mContext, "获取失败，" + e.message)
                }
            }
        })
    }

    /**
     *  @time 2025/8/7  20:12
     *  @author Im_jingwei
     *  @desc 弹出弹窗
     */
    private fun showPaymentWindow(price: String, type: String) {
        if (paymentWindow.isShowing) {
            paymentWindow.dismiss()
        } else {
            paymentWindow.showAtLocation(rootView, Gravity.BOTTOM, 0, 0, price, type)
        }
    }

}